package objects;



import containers.Canvas;
import java.awt.geom.Point2D;

/**
 * Directional wall repulsion that can be turned on and off.
 * Force applied is calculated exponentially.
 * @author rwb10 and sab60
 *
 */
public class WallRepulsion {
    private static Canvas ourCanvas;
    private int myID;
    private double myMagnitude;
    private double myExponent;
    private boolean myForceOn = false;

    /**
     * Constructor.
     * @param id      Identifying integer that corresponds to direction.
     * @param mag       Magnitude of force applied.
     * @param exp       Exponent of force applied based on distance.
     */
    public WallRepulsion (int id, double mag, double exp) {
        this.myID = id;
        myMagnitude = mag;
        myExponent = exp;
    }

    /**
     * Calculates the force applied to a mass.
     * @param mass      The mass being acted on.
     * @return          The force acting on the mass.
     */
    public Force getForce (Mass mass) {
        if (myForceOn) {
            double direction = getDirection();
            double magnitude = getMagnitude(mass.getCenter());
            return new Force(direction, magnitude);
        }
        else {
            return new Force(0, 0);
        }
    }

    private double getDirection () {
        switch (myID) {
            case 1:
                // top wall
                return 90;
            case 2:
                // right wall
                return 180;
            case 3:
                // bottom wall
                return 270;
            case 4:
                // left wall
                return 0;
            default:
                // a safegaurd
                System.out.printf("Exponent WARNING: id was not an expected value (1-4)," +
                                  "\neID = %d\n\n", myID);
                return 0;
        }
    }

    private double getMagnitude (Point2D point) {
        double distance;
        switch (myID) {
            case 1:
                // top wall
                distance = ourCanvas.getHeight() - point.getY();
                break;
            case 2:
                // right wall
                distance = point.getX();
                break;
            case 3:
                // bottom wall
                distance = point.getY();
                break;
            case 4:
                // left wall
                distance = ourCanvas.getWidth() - point.getX();
                break;
            default:
                // a safegaurd
                System.out.printf("Magnitude WARNING: id was not an expected value (1-4)," +
                                  "\nmID = %d\n\n", myID);
                distance = 0;
                break;
        }
        return Math.pow(distance * myMagnitude, myExponent);
    }

    /**
     * Sets the canvas container.
     * @param myCanvas  Container canvas for WallRepulsions
     */
    public static void setCanvas (Canvas myCanvas) {
        WallRepulsion.ourCanvas = myCanvas;
    }

    /**
     * For comparisons.
     * @param id        The other Wall's ID
     * @return          Whether or not the ID's match.
     */
    public boolean match (int id) {
        return myID == id;
    }

    /**
     * ID access method.
     * @return  The ID integer.
     */
    public int getID () {
        return myID;
    }

    /**
     * Toggle the force on or off.
     */
    public void toggle () {
        if (myForceOn) {
            myForceOn = false;
            System.out.printf("turning wall %d Off.\n", myID);
        }
        else {
            myForceOn = true;
            System.out.printf("turning wall %d On.\n", myID);
        }
    }

}
